<?php
namespace app\admin\controller;
use app\admin\logic\WorkRecordNoticeLogic;

/**
 * 通知公告api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/20
 * Time: 11:32
 */
class WorkRecordNotice extends Base {

    public function __construct(){
        parent::__construct();
        $this->wrn_logic = new WorkRecordNoticeLogic();
    }

    /**
     * 获取通知公告
     * @return \think\response\Json
     */
    public function ajax_get_wrn(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //获取通知公告
        $r = $this->wrn_logic->get_wrn($params);

        $data['list'] = $r['data']['list'];
        $data['total_num'] = $r['data']['total_num'];
        return json($this->wrn_logic->get_response(1,'OK',$data));
    }

    /**
     * 导出通知公告
     * http://127.0.0.1/mandiao-mini-api/public/admin-work_record_notice-export_wrn?token=e916a6039bff505bacaf5def2bb8435f&page=1&rows=10&department=全部&start_time=&end_time=
     * @return \think\response\Json
     */
    public function export_wrn(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //获取通知公告
        $r = $this->wrn_logic->get_wrn($params);
        $list = $r['data']['list'];

        $row = ['部门名称','发布人','名称','公告内容','发布时间','抄送对象'];

        $export_data = [];
        foreach($list as $k => $v){
            $export_data[$k]['department'] = $v['department'];
            $export_data[$k]['username'] = $v['username'];
            $export_data[$k]['name'] = $v['name'];
            $export_data[$k]['content'] = $v['content'];
            $export_data[$k]['c_time'] = $v['c_time'];
            $export_data[$k]['copy_username'] = $v['copy_username'];
        }
        array_unshift($export_data,$row);

        $file_name = '通知公告' . date('YmdHi') . '.xlsx';
        $r = $this->wrn_logic->gen_wrn_excel($file_name,$export_data);

        //$this->redirect(\think\facade\Request::root(true) . '/' . $file_name);
        return json($this->wrn_logic->get_response(1,'导出成功'));
    }
}